package com.mall.b2c.controller;

import com.mall.b2c.bean.IntergralShopProductBean;
import com.mall.b2c.domain.goods.GoodsDomain;
import com.mall.b2c.domain.intergral.ShopClassDomain;
import com.mall.b2c.domain.intergral.ShopProductDomain;
import com.mall.b2c.service.IntergralShopClassService;
import com.mall.b2c.service.IntergralShopProductService;
import com.mall.b2c.util.JsonDataGridHelper;
import com.mall.common.bean.Message;
import com.mall.common.bean.Pager;
import com.mall.common.bean.SearchFilter;
import com.mall.common.controller.BaseController;
import com.mall.common.domain.content.AdvertDomain;
import com.mall.common.domain.content.AdvertPositionDomain;
import com.mall.common.exception.NoticeException;
import com.mall.common.util.ResponseUtils;
import com.mall.common.web.Servlets;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by chen on 16/12/15.
 */

@Controller
@RequestMapping("/admin/b2c/intergral_shop_product")
public class IntergralShopProductController extends BaseController {
    @Resource
    IntergralShopProductService intergralShopProductService;
    @Resource
    IntergralShopClassService intergralShopClassService;


    /**
     * 获取列表信息
     */
    @RequestMapping(value="/dd", method=RequestMethod.GET)
    @ResponseBody
    public List dd(HttpServletRequest request){

        List  <IntergralShopProductBean> list = intergralShopProductService.getShoproductList(1L,"id asc");
        return   list;
    }

    /**
     *
     * 跳转到列表页面
     */
    @RequestMapping(value="/list", method= RequestMethod.GET)
    public String list(Model model)
    {

        return "/admin/b2c/intergralShoProduct/list";
    }

    @RequestMapping(value="/save", method=RequestMethod.POST)
    @ResponseBody
    public String save(ShopProductDomain  shopProductDomain, HttpServletResponse response){
        ShopClassDomain shopclass = intergralShopClassService.find(shopProductDomain.getShopclass().getId());
        if(shopclass == null){
            throw new NoticeException("不存在的分类");
        }
        if(!intergralShopProductService.isUnique("name", null, shopProductDomain.getName())){
            throw new NoticeException("该产品名已经存在!");
        }
        //advertDomain.setClickNumber(0);
        intergralShopProductService.persist(shopProductDomain);
        return ResponseUtils.renderJSON(successMessage, response);
    }
    @RequestMapping(value="/update", method=RequestMethod.POST)
    public String update(ShopProductDomain  shopProductDomain, Model model ,HttpServletResponse response){
        ShopClassDomain shopclass = intergralShopClassService.find(shopProductDomain.getShopclass().getId());
        if(shopclass == null){
            throw new NoticeException("不存在的分类");
        }

        intergralShopProductService.update(shopProductDomain);

        return ResponseUtils.renderJSON(successMessage, response);
    }

    /**
     * 获取列表信息
     */
    @RequestMapping(value="/queryData", method=RequestMethod.POST)
    @ResponseBody
    public Pager queryData(Pager pager, HttpServletRequest request){
        Map<String, Object> filterParams = Servlets.getParametersStartingWith(request);
        List<SearchFilter> filters = SearchFilter.parse(filterParams);
        pager = intergralShopProductService.findPage(pager, filters);
        return JsonDataGridHelper.createJSONData(pager);
    }

    /**
     * 修改产品
     * @param id 会员id
     * @param model
     * @return
     */
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model){
        model.addAttribute("shopclassList", intergralShopClassService.getAll());
        ShopProductDomain shoproduct = intergralShopProductService.find(id);
        if(shoproduct == null){
            throw new NoticeException("产品不存在!");
        }
        model.addAttribute("product", shoproduct);
        return "/admin/b2c/intergralShoProduct/input";
    }
    @RequestMapping(value="/add", method= RequestMethod.GET)
    public String add(Model model)
    {
        model.addAttribute("shopclassList", intergralShopClassService.getAll());
        return "/admin/b2c/intergralShoProduct/input";
    }

    @RequestMapping(value="/shopclassTree", method=RequestMethod.GET)
    public String goodsCategoryTree(Model model){
        return "/admin/b2c/intergralShoProduct/shopclassTree";
    }
}
